// src/router/index.ts
import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";
import Feedback from "../views/Feedback.vue";
import News from "../views/News.vue";
import Login from "../views/Login.vue";
import Register from "@/views/Register.vue";
import Profile from "@/views/Profile.vue";
import UpdatePwd from "@/views/UpdatePwd.vue";
import AddHouse from "@/views/Landlord/AddHouse.vue";
import Houselist from "@/views/Landlord/Houselist.vue";
import UpadteHouse from "@/views/Landlord/UpadteHouse.vue";
import FavoritesList from "@/views/Tenant/FavoritesList.vue";
import HouseDetails from "@/views/HouseDetails.vue";
import MyFeedback from "@/views/myFeedback.vue";
import Order from "@/views/Order.vue";
import contract from "@/views/Tenant/Contract.vue";
import Pay from "@/views/Tenant/Pay.vue";

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    meta: {
      title: "首页",
    },
  },
  {
    path: "/feedback",
    name: "Feedback",
    component: Feedback,
    meta: {
      title: "用户反馈",
    },
  },
  {
    path: "/news",
    name: "News",
    component: News,
    meta: {
      title: "房屋资讯",
    },
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "用户登录",
      guestOnly: true,
    },
  },
  {
    path: "/register",
    name: "Register",
    component: Register,
    meta: {
      title: "用户注册",
      guestOnly: true,
    },
  },
  {
    path: "/profile",
    name: "Profile",
    component: Profile,
    meta: {
      title: "个人信息",
    },
  },
  {
    path: "/updatePwd",
    name: "UpdatePwd",
    component: UpdatePwd,
    meta: {
      title: "修改密码",
    },
  },
  {
    path: "/addHouse",
    name: "AddHouse",
    component: AddHouse,
    meta: {
      title: "添加房屋",
    },
  },
  {
    path: "/Houselist",
    name: "Houselist",
    component: Houselist,
    meta: {
      title: "房屋列表",
    },
  },
  {
    path: "/updateHouse/:id",
    name: "UpadteHouse",
    component: UpadteHouse,
    meta: {
      title: "修改房屋",
    },
  },
  {
    path: "/favoritesList",
    name: "FavoritesList",
    component: FavoritesList,
    meta: {
      title: "我的收藏",
    },
  },
  {
    path: "/houses/:id",
    name: "HouseDetail",
    component: HouseDetails,
    props: (route) => ({ id: route.params.id }),
    meta: { title: "房屋详情" },
  },
  {
    path: "/myFeedback",
    name: "MyFeedback",
    component: MyFeedback,
    meta: {
      title: "我的反馈",
    },
  },
  {
    path: "/order",
    name: "Order",
    component: Order,
    meta: {
      title: "我的订单",
    },
  },
  {
    path: "/contract",
    name: "contract",
    component: contract,
    meta: {
      title: "合同",
    },
  },
  {
    path: "/pay",
    name: "Pay",
    component: Pay,
    meta: {
      title: "支付",
    },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  document.title = `${to.meta.title} | 房屋租赁系统`;

  const token = localStorage.getItem("token");
  const isAuthenticated = !!token;

  // 检查是否需要身份验证
  if (to.matched.some((record) => record.meta.guestOnly)) {
    if (isAuthenticated) {
      next(); // 已登录用户可正常访问（如查看帮助页面）
    } else {
      next(); // 未登录用户允许访问
    }
  } else if (to.matched.some((record) => record.meta.guestOnly)) {
    if (isAuthenticated) {
      next({ name: "Home" });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
